package com.tekj.cxqcb.view.Module.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.squareup.picasso.Picasso;
import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.config.enums.EventBusMsgBean;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.PubStatusBean;
import com.tekj.cxqcb.operation.resultBean.UploadFileBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.operationHelper.Bean.BindingViewBean;
import common.tool.Tools;
import common.widget.CircleTransform;
import es.dmoral.toasty.Toasty;

import static com.tekj.cxqcb.config.enums.EventBusMsgBean.EnumStatus.个人信息修改;

/**
 * 账户与安全
 */
public class AccountActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.img_sdv_head)
    SimpleDraweeView imgSdvHead;
    @BindView(R.id.ll_head_picture)
    LinearLayout llHeadPicture;
    @BindView(R.id.et_nickname)
    EditText etNickname;
    @BindView(R.id.ll_nickname)
    LinearLayout llNickname;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.ll_phone)
    LinearLayout llPhone;
    @BindView(R.id.tv_wx)
    TextView tvWx;
    @BindView(R.id.ll_WX)
    LinearLayout llWX;
    @BindView(R.id.ll_password)
    LinearLayout llPassword;
    @BindView(R.id.ll_pay_password)
    LinearLayout llPayPassword;
    @BindView(R.id.ll_Cancellation)
    LinearLayout llCancellation;

    BusinessDao businessDao;
    String name, imgeString;

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void init() {
        EventBus.getDefault().register(this);
        businessDao = new BusinessDao(this, this);
        tvTitle.setText("账户与安全");
        tvR.setText("保存");
        setUserData();

    }

    void setUserData() {
        tvId.setText(MainBActivity.User.getUserId());
        imgSdvHead.setImageURI(MainBActivity.User.getHead());
        etNickname.setText(MainBActivity.User.getNickname());
        tvPhone.setText(MainBActivity.User.getPhone());
        Picasso.get()
                .load(MainBActivity.User.getHead().equals("") ? "1" : MainBActivity.User.getHead())
                .transform(new CircleTransform())
                .error(R.mipmap.icon_head)
                .placeholder(R.mipmap.icon_head)
                .into(imgSdvHead);
    }

    @Override
    public int setLayout() {
        return R.layout.activity_account;
    }

    @Override
    public void initLoadStart() {
        showDialog();
    }

    @Override
    public void initLoadEnd() {
        dialog.dismiss();
    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 修改用户信息:
                PubStatusBean pubStatusBean = (PubStatusBean) bindingViewBean.getBean();
                if ("0000000".equals(pubStatusBean.getCode())) {
                    EventBus.getDefault().post(new EventBusMsgBean(个人信息修改, "修改用户信息"));
                    finish();
                } else
                    Toasty.normal(this, pubStatusBean.getMsg()).show();

                break;
            case 单文件上传:
                UploadFileBean statusBean = (UploadFileBean) bindingViewBean.getBean();
                if ("0000000".equals(statusBean.getCode())) {
                    imgeString = statusBean.getData().getUploadFilePath();
                } else {
                    Toasty.normal(this, statusBean.getMsg()).show();
                }

                break;

        }
    }


    @OnClick({R.id.ll_back, R.id.img_sdv_head, R.id.ll_nickname,
            R.id.ll_phone, R.id.ll_WX, R.id.ll_password,
            R.id.ll_pay_password, R.id.ll_Cancellation, R.id.ll_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.ll_right:
                name = etNickname.getText().toString().trim();
                if (name.equals("")) {
                    Toasty.normal(activity, "请输入昵称").show();
                    return;
                }

                Tools.showNormalDialogOne(activity, "修改个人信息", "确认修改？", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        businessDao.UpdateUserInfo(MainBActivity.User.getUserId(), "2", imgeString, name);
                    }
                }, null);
                break;
            case R.id.img_sdv_head:
                Tools.InitPictureSelector(activity, false, 100);
                break;
            case R.id.ll_nickname:

                break;
            case R.id.ll_phone:
                startActivity(new Intent(activity, PhoneBindingActivity.class));
                break;
            case R.id.ll_WX:

                break;
            case R.id.ll_password:
                startActivity(new Intent(activity, PasswordActivity.class));
                break;
            case R.id.ll_pay_password:
                startActivity(new Intent(activity, PayPasswordActivity.class));
                break;
            case R.id.ll_Cancellation:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 100:
                    LocalMedia imge = PictureSelector.obtainMultipleResult(data).get(0);
                    Picasso.get().load(new File(imge.getCompressPath()))
                            .transform(new CircleTransform()).into(imgSdvHead);
                    businessDao.UploadFile(imge.getCompressPath());
                    break;
            }
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void Event(EventBusMsgBean e) {
        switch (e.getmEnumStatus()) {
            case 登陆成功刷新:
                setUserData();
                break;

        }
    }
}
